#include "SpanSequence.h"
#include <xml/Saver.h>

namespace liba {
namespace lib3d {
namespace anim2d {

SpanSequence::SpanSequence()
	: total_frames_count(0)
{}

SpanSequence::SpanSequence(const SpanSequence &rhs)
	: total_frames_count(rhs.total_frames_count)
	, AbstractSequence(rhs)
{}

AbstractSequence* SpanSequence::clone() const
{
	return new SpanSequence(*this);
}

void SpanSequence::render_to_list(hard::Hardware * hardware, hard::RenderTexture * render_texture, unsigned frame_number)
{
	tex_sequence->change_frame(frame_number);
	tex_sequence->render_to_list(hardware, render_texture);
	return;
}

void SpanSequence::on_save_attributes(xml::SaverBase * saver) const
{
	AbstractSequence::on_save_attributes(saver);

	if(total_frames_count != 0)
		saver->save_attribute("FramesInFile", total_frames_count);
}

bool SpanSequence::on_attribute(xml::Provider * prov, const std::string & name, const std::string & value)
{
	if( name == "FramesInFile" )
		return converter::convert( value, &total_frames_count );
	return AbstractSequence::on_attribute(prov, name, value);
}

size_t SpanSequence::get_frames_count() const
{
	return tex_sequence->get_frames_count();
}

void SpanSequence::load_image(const std::string & image_name)
{
	material::TextureSequence::Name nam;
	nam.image = filesystem::String( image_name );
	nam.frame_start = frame_start;
	nam.frame_end = frame_end;
	nam.total_frames_count = total_frames_count;
	nam.wrap = false;
	tex_sequence.reset(new material::TextureSequence(Atom("Diffuse"), nam));
}

} // namespace anim2d
} // namespace lib3d
} // namespace liba
